package com.tcm.pay.request;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.XmlUtil;
import com.base.core.network.SimpleHttpUtils;
import com.base.core.network.http.ResponseEntity;
import com.base.core.utils.JacksonUtils;
import com.base.core.utils.R;
import com.tcm.pay.common.HttpRequest;
import com.tcm.pay.domain.dto.AggregatePayDTO;
import com.tcm.pay.enums.BusinessCodeEnum;
import com.tcm.pay.util.Base64Util;
import com.tcm.pay.util.RandomUtil;
import org.springframework.stereotype.Component;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

@Component
public class WxAggregatePay implements AggregatePay{

    @Override
    public Map<String, Object> pay(Object payObject) {
        AggregatePayDTO aggregatePayDTO = (AggregatePayDTO) payObject;
        String url = "https://gwapi.yemadai.com/pay/aggregatePay";

        if(ObjectUtil.isEmpty(aggregatePayDTO.getMerchantOrderNo())){
            R.throwFail(BusinessCodeEnum.MERCANT_ORDER_NO_NOT_EXISTS);
        }
        aggregatePayDTO.setMerchantNo("50829");
        aggregatePayDTO.setRandomStr(RandomUtil.getStringRandom(15));
        aggregatePayDTO.setPayType("WxJsapi_OnLine");
        if(ObjectUtil.isEmpty(aggregatePayDTO.getAmount())){
            aggregatePayDTO.setAmount("0.01");
                                                                                                                             }
        if(ObjectUtil.isEmpty(aggregatePayDTO.getSubject())){
            aggregatePayDTO.setSubject("药材");
        }
        if(ObjectUtil.isEmpty(aggregatePayDTO.getDesc())){
            aggregatePayDTO.setDesc("药品费用支付");
        }
        aggregatePayDTO.setCompanyNo("sweep-4203d9624f714b1fa245fbe67fbc318a");
        if(ObjectUtil.isEmpty(aggregatePayDTO.getAdviceUrl())){
            aggregatePayDTO.setAdviceUrl("http://zetikeji.com/");
        }
        System.out.println("回调地址："+aggregatePayDTO.getAdviceUrl());
        aggregatePayDTO.setSubAppid("wx9d9bf3ce52169f1f");

        StringBuffer xml = new StringBuffer();
        xml.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
        xml.append("<AggregatePayRequest>");
        xml.append("<MerchantNo>").append(aggregatePayDTO.getMerchantNo()).append("</MerchantNo>");
        xml.append("<MerchantOrderNo>").append(aggregatePayDTO.getMerchantOrderNo()).append("</MerchantOrderNo>");
        xml.append("<PayType>").append(aggregatePayDTO.getPayType()).append("</PayType>");
        xml.append("<Amount>").append(aggregatePayDTO.getAmount()).append("</Amount>");
        xml.append("<Subject>").append(aggregatePayDTO.getSubject()).append("</Subject>");
        xml.append("<Desc>").append(aggregatePayDTO.getDesc()).append("</Desc>");
        xml.append("<CompanyNo>").append(aggregatePayDTO.getCompanyNo()).append("</CompanyNo>");
        xml.append("<RandomStr>").append(aggregatePayDTO.getRandomStr()).append("</RandomStr>");
        xml.append("<SignInfo>").append(HttpRequest.makeSignForAggregatePay(aggregatePayDTO.getAdviceUrl(),aggregatePayDTO.getAmount(),aggregatePayDTO.getMerchantNo(),aggregatePayDTO.getMerchantOrderNo(),aggregatePayDTO.getPayType(),aggregatePayDTO.getRandomStr())).append("</SignInfo>");
        xml.append("<AdviceUrl>").append(aggregatePayDTO.getAdviceUrl()).append("</AdviceUrl>");
//        xml.append("<ReturnUrl>").append(aggregatePayDTO.getReturnUrl()).append("</ReturnUrl>");
        xml.append("<SubAppid>").append(aggregatePayDTO.getSubAppid()).append("</SubAppid>");
        xml.append("<UserId>").append(aggregatePayDTO.getUserId()).append("</UserId>");
        xml.append("</AggregatePayRequest>");

        String xmlData = Base64Util.encode(xml.toString());

        Map<String, String> params = new HashMap<>();
        params.put("requestDomain",xmlData);
        ResponseEntity res = SimpleHttpUtils.post(url,params,false);
        byte[] resData = Base64Util.decode(res.getBody());
        String resXml = new String(resData, StandardCharsets.UTF_8);
        return XmlUtil.xmlToMap(resXml);

    }


}
